/*
 * WButton.java
 *
 * Created on 22 October 2007, 22:08
 *
 */

package net.ubinow.mflex.widgets;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import net.ubinow.mflex.baseui.AbsWidget;
import net.ubinow.mflex.baseui.WFont;
import net.ubinow.mflex.data.SimpleDataItem;
import net.ubinow.mflex.events.Event;
import net.ubinow.mflex.events.EventFactory;
import net.ubinow.mflex.util.Constants;
import net.ubinow.mflex.util.KeyConstants;

/**
 * 
 * @author Josh
 */
public class WButton extends AbsWidget {
   
    // TODO: handle tickering text - for text that is too long for the button 
    
    private int _textPaddingX = Constants.AD_TEXT_PADDING_X;
    private int _textPaddingY = Constants.AD_TEXT_PADDING_Y;
    
    private WFont _font = null;
    private Image _imgNormal = null;
    private Image _imgSelected = null;
    private Image _imgDown = null;
    
    ///
    // text positioning
    ///
    private int _textAlignment = 0;
    private int _textX = -1;
    private int _textY = -1;
    
    ///
    // button icon 
    ///
    private Image _icon = null;
    private int _iconX = -1;
    private int _iconY = -1; 
    private int _iconAlignment = Constants.ALIGN_LEFT | Constants.ALIGN_CENTRE_V;
    private int _iconPaddingX = 0;
    private int _iconPaddingY = 0; 
    
    /** Creates a new instance of WButton */
    public WButton( WFont customFont, Image normal, Image selected, Image down ) {
        _font = customFont;
        _imgNormal = normal;
        _imgSelected = selected;
        _imgDown = down;
        
        _width = _imgNormal.getWidth();
        _height = _imgNormal.getHeight(); 
        
        _textAlignment = Constants.ALIGN_CENTRE_V | Constants.ALIGN_CENTRE_H;
    }
    
    public WButton( String id, WFont customFont, Image normal, Image selected, Image down ) {
        this( customFont, normal, selected, down );
        _id = id;
    }
    
    public void setText( String text ){
        
        _textX = -1; // realign text
        
        if( _items == null ){
            addItem( new SimpleDataItem( text ) );
        } else{
            getSelectedItem().setText( text );
        }
        
        _textSet = true; 
    }
    
    private boolean _textSet = false; 
    
    /** set the text alignment **/ 
    public void setAlignment( int alignment ){
        _textAlignment = alignment;
        
        // reset positioning
        _textX = _textY = -1;
    }
    
    public boolean handleKeyPressed(int action, int keyCode) {
                
        if( action == Canvas.FIRE || keyCode == KeyConstants.ENTER_BUTTON ){
            Event evt = EventFactory.getInstance().getEvent( Event.EVT_SELECT_DOWN ); 
            evt._publisher = this; 
            notifySubscribers( evt );
            return true;
        } else{
            return false;
        }
    }
    
    public boolean handleKeyReleased( int action, int keyCode ){
        return false; 
    }
    
    public void cycle() {
    }
    
    public void paint(Graphics g, int offsetX, int offsetY, int layer) {        
        if( !_isVisible )
            return; 
        
        if( _focus ){
            g.drawImage( _imgSelected, _x + offsetX, _y + offsetY, Constants.GFX_ALIGN_TOP_LEFT );
        } else{
            g.drawImage( _imgNormal, _x + offsetX, _y + offsetY, Constants.GFX_ALIGN_TOP_LEFT );
        }
        
        if( _icon != null ){
            // draw icon 
            if( _iconX == -1 )
                realignIconX();

            if( _iconY == -1 )
                realignIconY();

            g.drawImage( _icon, _iconX + offsetX, _y + _iconY + offsetY, Constants.GFX_ALIGN_TOP_LEFT );
        }
        
        if( _textSet ){
            // draw text
            if( _textX == -1 ){
                realignTextX();
            }

            if( _textY == -1 ){
                realignTextY();
            }

            _font.drawString( g, getSelectedText(), _textX + offsetX, _y + _textY + offsetY );
        }
    }
    
    private void realignIconX(){
        // work out text position x
        if( (_iconAlignment & Constants.ALIGN_LEFT) == Constants.ALIGN_LEFT ){
            _iconX = _x+_iconPaddingX;
        } else if( (_iconAlignment & Constants.ALIGN_RIGHT) == Constants.ALIGN_RIGHT ){
            _iconX = _x-_iconPaddingX+(_width-_icon.getWidth() ); 
        } else if( (_iconAlignment & Constants.ALIGN_CENTRE_H) == Constants.ALIGN_CENTRE_H ){
            _iconX = _x + (_width-_icon.getWidth())>>1; 
        }
    }        
    
    private void realignIconY(){
        // work out text position x
        if( (_iconAlignment & Constants.ALIGN_TOP) == Constants.ALIGN_TOP ){
            _iconY = _iconPaddingY; 
        } else if( (_iconAlignment & Constants.ALIGN_CENTRE_V) == Constants.ALIGN_CENTRE_V ){
            _iconY = ((_height-_icon.getHeight())>>1); 
        } else if( (_iconAlignment & Constants.ALIGN_BOTTOM) == Constants.ALIGN_BOTTOM ){
            _iconY = _height-_iconPaddingY-_icon.getHeight(); 
        }
    }
    
    private void realignTextX(){
        // work out text position x
        if( (_textAlignment & Constants.ALIGN_LEFT) == Constants.ALIGN_LEFT ){
            _textX = _x+_textPaddingX;
        } else if( (_textAlignment & Constants.ALIGN_RIGHT) == Constants.ALIGN_RIGHT ){
            _textX = _x-_textPaddingX+(_width-_font.stringWidth( getSelectedText() ) ); 
        } else if( (_textAlignment & Constants.ALIGN_CENTRE_H) == Constants.ALIGN_CENTRE_H ){
            _textX = _x + (_width-_font.stringWidth( getSelectedText())>>1); 
        }
    }        
    
    private void realignTextY(){
        // work out text position x
        if( (_textAlignment & Constants.ALIGN_TOP) == Constants.ALIGN_TOP ){
            _textY = _textPaddingY; 
        } else if( (_textAlignment & Constants.ALIGN_CENTRE_V) == Constants.ALIGN_CENTRE_V ){
            _textY = ((_height-_font.getHeight())>>1); 
        } else if( (_textAlignment & Constants.ALIGN_BOTTOM) == Constants.ALIGN_BOTTOM ){
            _textY = _height-_textPaddingY-_font.getHeight(); 
        }
    }    

    /** set the type of widget to widget type button **/ 
    protected void setWidgetType() {
        _type = Constants.WIDGET_TYPE_BUTTON;
    }

    public void setBG(Image bg) {
        // not implemented 
    }

    public void setBG(int colour) {
        // not implemented
    }    
    
    public void setPos( int x, int y ){
        // reset positioning
        _textX = _textY = -1;
        _iconX = _iconY = -1; 
        
        super.setPos( x, y );                
    }
    
    public void setX( int x ){
        _textX= -1;
        _iconX = -1;
        super.setX( x );
    }
    
    public void setY( int y ){
        _textY= -1;
        _iconY = -1;
        super.setY( y );
    }
    
    //<editor-fold defaultstate="collapsed" desc="icon methods">
    
    public void setIcon( Image icon, int paddingX, int paddingY, int iconAlignment ){
        _icon = icon;
        _iconPaddingX = paddingX;
        _iconPaddingY = paddingY; 
        _iconAlignment = iconAlignment;
        _iconX = _iconY = -1;        
    }
    
    //</editor-fold>
    
}
